package com.fram.controller.api;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.api.message.request.MessageDeleteRequest;
import com.fram.bean.api.message.request.MessageReadRequest;
import com.fram.bean.api.message.request.MessageSearchRequest;
import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.service.api.v1.MessageService;

/**
 * @作者： 陳曉鬆 @时间： 2020年6月2日 - 上午10:20:44 @description：->
 */
@RestController
@RequestMapping("/api/{version}/message")
public class MessageController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(MessageController.class);

	@Resource(name = "messageService")
	private MessageService messageService;

	@ApiVersion(1.0)
	@RequestMapping(value = "/list")
	private ResponseMsg list(@RequestBody PageBean<MessageSearchRequest> request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (request.getParam() == null) {
				request.setParam(new MessageSearchRequest());
			}
			request.getParam().setUserId(getApiUserId());
			msg.setData(messageService.list(request));
		} catch (ProException e) {
			LOG.error("-消息-获取消息列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-消息-获取消息列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/read")
	private ResponseMsg read(@RequestBody MessageReadRequest request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			messageService.read(request, getApiUserId());
		} catch (ProException e) {
			LOG.error("-消息-消息标记为已读、未读出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-消息-消息标记为已读、未读异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/delete")
	private ResponseMsg delete(@RequestBody MessageDeleteRequest request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			messageService.delete(request, getApiUserId());
		} catch (ProException e) {
			LOG.error("-消息-用户删除消息出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-消息-用户删除消息异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
